package com.test.es_java_api.component;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * 作者:  龚泽楷
 * 时间:  2024/1/25
 */
@Component
public class ElasticClient {

    @Bean
    ElasticsearchClient getElasticsearchClient(){
        //创建  low-level 客户端  请求入口
        RestClient restClient = RestClient
                .builder(new HttpHost("127.0.0.1", 9200))
                .build();

        //json映射器  dsl请求
        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());

        //java api 客户端
        return new ElasticsearchClient(transport);
    }
}
